﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

public partial class ManterAutor : System.Web.UI.Page
{
    public static Boolean inserir;
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        inserir = true;
        tbNome.Enabled = true;
        rblTipo.Enabled = true;
        btNovo.Enabled = true;
        btCancelar.Enabled = true;
        btGravar.Enabled = true;
        btNovo.Enabled = false;
    }
    private void cancelar()
    {
        tbCodigo.Text = "";
        tbNome.Text = "";        
        rblTipo.SelectedValue = "1";

        tbNome.Enabled = false;
        rblTipo.Enabled = false;
        btCancelar.Enabled = false;
        btGravar.Enabled = false;
        btNovo.Enabled = true;
        

    }

    protected void btCancelar_Click(object sender, EventArgs e)
    {
        cancelar();
    }
    protected void btGravar_Click(object sender, EventArgs e)
    {
        try
        {

            if (inserir)
            {
                Autor a = new Autor(tbNome.Text, Convert.ToInt32(rblTipo.SelectedValue));
                AutorBLL ab = new AutorBLL();
                ab.Insere(a);
            }
            else
            {
                Autor a = new Autor(Convert.ToInt32(tbCodigo.Text), tbNome.Text, Convert.ToInt32(rblTipo.SelectedValue));
                AutorBLL ab = new AutorBLL();
                ab.Atualiza(a);
            }

            cancelar();
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }
   
 
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

    }
    protected void lbRemove_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            Autor a = new Autor(id);
            AutorBLL ab = new AutorBLL();
            ab.Remove(a);
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }

    protected void lbAtualiza_Click(object sender, EventArgs e)
    {
        inserir = false;
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            Autor autor = new Autor(id);

            AutorBLL autorbll = new AutorBLL();
            autor = autorbll.Consulta(autor.Codigo);
            tbCodigo.Text = autor.Codigo.ToString();
            tbNome.Text = autor.Nome;
            rblTipo.SelectedValue = autor.Tipo.ToString();

            tbNome.Enabled = true;
            rblTipo.Enabled = true;
            btNovo.Enabled = true;
            btCancelar.Enabled = true;
            btGravar.Enabled = true;
            btNovo.Enabled = false;
        }
        catch (Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }
}
